package views.ebook.com;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import Dao.ebook.com.ReaderDao;
import tools.ComponentTools;
import tools.HttpClientUtil;

/**
 * 读者维护
 * 
 * @author 林沐
 *
 */
public class Readersmaintain extends JPanel implements ActionListener {
	private ReaderDao readerdao = new ReaderDao();
	private JLabel jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb8, jb9, jb10, jb11, jb12, jb13, jb14;
	private JTextField jt1, jt2, jt3, jt4, jt5, jt6, jt7, jt8;
	private JButton jbu1, jbu2, jbu3, jbu4, jbu5;
	private JRadioButton jr1, jr2;
	private JTextArea jte;
	private JToggleButton toggleBtn;
	private DefaultTableModel tableModel;
	private ButtonGroup gr;
	String t = "";
	private ComponentTools conn = new ComponentTools();

	public Readersmaintain() {
		this.add(this.ReadmatBox());
	}

	public Box ReadmatBox() {
		Box bx = Box.createVerticalBox();
		Box bx1 = Box.createHorizontalBox();
		jb1 = new JLabel("读者维护");
		jb1.setFont(new Font("楷体", Font.PLAIN, 40));
		bx1.add(jb1);

		Box bx2 = Box.createHorizontalBox();
		jb2 = new JLabel("读者姓名：");
		jb2.setFont(new Font("楷体", Font.PLAIN, 25));
		jt1 = new JTextField(15);
		jt1.setFont(new Font("楷体", Font.PLAIN, 25));
		jb3 = new JLabel("联系电话：");
		jb3.setFont(new Font("楷体", Font.PLAIN, 25));
		jt2 = new JTextField(15);
		jt2.setFont(new Font("楷体", Font.PLAIN, 25));
		jbu1 = new JButton("查询");
		jbu1.setFont(new Font("楷体", Font.PLAIN, 25));
		jbu2 = new JButton("重置");
		jbu2.setFont(new Font("楷体", Font.PLAIN, 25));
		bx2.add(jb2);
		bx2.add(jt1);
		bx2.add(Box.createHorizontalStrut(10));
		bx2.add(jb3);
		bx2.add(jt2);
		bx2.add(Box.createHorizontalStrut(10));
		bx2.add(jbu1);
		bx2.add(Box.createHorizontalStrut(10));
		bx2.add(jbu2);

		jbu1.addActionListener(this);
		jbu2.addActionListener(this);

		Box bx3 = Box.createHorizontalBox();
		String[] colnames = { "编号", "读者姓名", "密码", "性别", "注册时间", "联系电话", "联系地址", "会员状态", "备注信息" };
		String sql = "select * from tb_reader";
		ArrayList plist = readerdao.getRecordsDataBySql(sql);
		String[][] rowdatas = readerdao.ListToArray(plist);
		JTable mytable = new JTable();// 创建表格
		tableModel = new DefaultTableModel(rowdatas, colnames);
		mytable.setModel(tableModel);

		// 列表获取值
		mytable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			// 列表的监听事件
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				// TODO Auto-generated method stub
				// 得到当前行
				int rowIndex = mytable.getSelectedRow();// 得到当前行号
				// 根据行号获取指定列 列好从0开始
				if (rowIndex == -1) {
					RefreshTable();
				} else {
					// 编号
					String id = mytable.getValueAt(rowIndex, 0).toString();
					jt3.setText(id);
					// 姓名
					String readname = mytable.getValueAt(rowIndex, 1).toString();
					jt4.setText(readname);
					// 密码
					String pwd = mytable.getValueAt(rowIndex, 2).toString();
					jt5.setText(pwd);
					// 性别
					String sex = mytable.getValueAt(rowIndex, 3).toString();
					if (sex.equals("男")) {
						jr1.setSelected(true);
					} else {
						jr2.setSelected(true);
					}
					// 注册时间
					String retime = mytable.getValueAt(rowIndex, 4).toString();
					jt6.setText(retime);
					// 联系电话
					String phone = mytable.getValueAt(rowIndex, 5).toString();
					jt7.setText(phone);
					// 地址
					String addre = mytable.getValueAt(rowIndex, 6).toString();
					jt8.setText(addre);
					// 会员状态
					String hui = mytable.getValueAt(rowIndex, 7).toString();
					if (hui.equals("1")) {
						toggleBtn.setText("启用");
						toggleBtn.setSelected(true);
					} else {
						toggleBtn.setText("禁用");
						toggleBtn.setSelected(false);
					}
					// 备注
					String bz = mytable.getValueAt(rowIndex, 8).toString();
					jte.setText(bz);
				}
			}
		});

		mytable.setPreferredScrollableViewportSize(new Dimension(1300, 300));
		mytable.setFont(new Font("楷体", Font.PLAIN, 25));// 设置表格字体大小
		mytable.setRowHeight(30);// 设置行高
		mytable.getTableHeader().setFont(new Font("楷体", Font.PLAIN, 20));// 设置表头的字体
//		mytable.getTableHeader().setForeground(Color.red);//设置字体颜色
		mytable.getTableHeader().setResizingAllowed(false);// 列大小不可以改变
		mytable.getTableHeader().setReorderingAllowed(false);// 表头不可以移动
		JScrollPane sorc = new JScrollPane(mytable);// 滑动条
		// 内容居中
		DefaultTableCellRenderer r = new DefaultTableCellRenderer();
		r.setHorizontalAlignment(JLabel.CENTER);
		mytable.setDefaultRenderer(Object.class, r);
		// 表头居中
		((DefaultTableCellRenderer) mytable.getTableHeader().getDefaultRenderer())
				.setHorizontalAlignment(JLabel.CENTER);

		bx3.add(sorc);

		Box bx4 = Box.createHorizontalBox();
		jb4 = new JLabel("编号：");
		jb4.setFont(new Font("楷体", Font.PLAIN, 25));
		jt3 = new JTextField(10);
		jt3.setEnabled(false);
		jt3.setFont(new Font("楷体", Font.PLAIN, 25));
		jb5 = new JLabel("姓名：");
		jb5.setFont(new Font("楷体", Font.PLAIN, 25));
		jt4 = new JTextField(15);
		jt4.setFont(new Font("楷体", Font.PLAIN, 25));
		jb6 = new JLabel("读者密码：");
		jb6.setFont(new Font("楷体", Font.PLAIN, 25));
		jt5 = new JTextField(15);
		jt5.setFont(new Font("楷体", Font.PLAIN, 25));
		jb7 = new JLabel("性别：");
		jb7.setFont(new Font("楷体", Font.PLAIN, 25));
		ImageIcon backIcon = new ImageIcon("src/images/readerman.png");
		jr1 = new JRadioButton("男");
		jr1.setFont(new Font("楷体", Font.PLAIN, 25));
		jb8 = new JLabel(backIcon);
		ImageIcon backIcon2 = new ImageIcon("src/images/readergir.png");
		jr2 = new JRadioButton("女");
		jr2.setFont(new Font("楷体", Font.PLAIN, 25));
		jb9 = new JLabel(backIcon2);
		gr = new ButtonGroup();
		gr.add(jr1);
		gr.add(jr2);
		jb10 = new JLabel("当前状态：");
		jb10.setFont(new Font("楷体", Font.PLAIN, 25));
		toggleBtn = new JToggleButton("禁用");
//		toggleBtn.setText("禁用");
		toggleBtn.setFont(new Font("楷体", Font.PLAIN, 20));
		// 设置 选中(开) 和 未选中(关) 时显示的图片
		toggleBtn.setSelectedIcon(new ImageIcon("src/images/opent.png"));
		toggleBtn.setIcon(new ImageIcon("src/images/close7.png"));

		toggleBtn.addActionListener(this);

		bx4.add(jb4);
		bx4.add(jt3);
		bx4.add(Box.createHorizontalStrut(10));
		bx4.add(jb5);
		bx4.add(jt4);
		bx4.add(Box.createHorizontalStrut(10));
		bx4.add(jb6);
		bx4.add(jt5);
		bx4.add(Box.createHorizontalStrut(10));
		bx4.add(jb7);
		bx4.add(jr1);
		bx4.add(jb8);
		bx4.add(Box.createHorizontalStrut(5));
		bx4.add(jr2);
		bx4.add(jb9);
		bx4.add(Box.createHorizontalStrut(10));
		bx4.add(jb10);
		bx4.add(toggleBtn);

		Box bx5 = Box.createHorizontalBox();
		jb11 = new JLabel("注册时间：");
		jb11.setFont(new Font("楷体", Font.PLAIN, 25));
		jt6 = new JTextField(15);
		jt6.setFont(new Font("楷体", Font.PLAIN, 25));
		jb12 = new JLabel("联系电话：");
		jb12.setFont(new Font("楷体", Font.PLAIN, 25));
		jt7 = new JTextField(15);
		jt7.setFont(new Font("楷体", Font.PLAIN, 25));
		jb13 = new JLabel("联系地址：");
		jb13.setFont(new Font("楷体", Font.PLAIN, 25));
		jt8 = new JTextField(15);
		jt8.setFont(new Font("楷体", Font.PLAIN, 25));
		bx5.add(jb11);
		bx5.add(jt6);
		bx5.add(Box.createHorizontalStrut(10));
		bx5.add(jb12);
		bx5.add(jt7);
		bx5.add(Box.createHorizontalStrut(10));
		bx5.add(jb13);
		bx5.add(jt8);

		Box bx6 = Box.createHorizontalBox();
		jb14 = new JLabel("备注信息：");
		jb14.setFont(new Font("楷体", Font.PLAIN, 25));
		jte = new JTextArea(3, 20);
		jte.setFont(new Font("楷体", Font.PLAIN, 25));
		JScrollPane jsp = new JScrollPane(jte);
		bx6.add(jb14);
		bx6.add(jsp);

		Box bx7 = Box.createHorizontalBox();
		jbu3 = new JButton("修改", new ImageIcon("src/images/readerup.png"));
		jbu3.setFont(new Font("楷体", Font.PLAIN, 25));
		jbu4 = new JButton("删除", new ImageIcon("src/images/readerde.png"));
		jbu4.setFont(new Font("楷体", Font.PLAIN, 25));
		jbu5 = new JButton("重置", new ImageIcon("src/images/readerrest.png"));
		jbu5.setFont(new Font("楷体", Font.PLAIN, 25));
		bx7.add(jbu3);
		bx7.add(Box.createHorizontalStrut(30));
		bx7.add(jbu4);
		bx7.add(Box.createHorizontalStrut(30));
		bx7.add(jbu5);

		// 事件监听
		jbu1.addActionListener(this);
		jbu2.addActionListener(this);
		jbu3.addActionListener(this);
		jbu4.addActionListener(this);
		jbu5.addActionListener(this);

		bx.add(Box.createVerticalStrut(10));
		bx.add(bx1);
		bx.add(Box.createVerticalStrut(25));
		bx.add(bx2);
		bx.add(Box.createVerticalStrut(15));
		bx.add(bx3);
		bx.add(Box.createVerticalStrut(15));
		bx.add(bx4);
		bx.add(Box.createVerticalStrut(15));
		bx.add(bx5);
		bx.add(Box.createVerticalStrut(20));
		bx.add(bx6);
		bx.add(Box.createVerticalStrut(20));
		bx.add(bx7);
		return bx;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// 单选框
		String n = "";
		if (jr1.isSelected()) {// 获取单选按钮的值
			n = jr1.getText();
		} else {
			n = jr2.getText();
		}

		// 开关按钮
		if (e.getSource() == toggleBtn) {// 开关按钮
			if (toggleBtn.isSelected()) {
				toggleBtn.setText("启用");
				t = "1";
			} else {
				toggleBtn.setText("禁用");
				t = "0";
			}
		}
		// TODO Auto-generated method stub
		String id = jt3.getText();// 编号
		String name = jt4.getText();// 姓名
		String pwd = jt5.getText();// 密码
		String tiem = jt6.getText();// 注册时间
		String phone = jt7.getText();// 联系电话
		String address = jt8.getText();// 地址
		String bz = jte.getText();// 备注
		if (e.getSource() == jbu2) {
			// 上重置
			jt1.setText("");
			jt2.setText("");
			RefreshTable();
		} else if (e.getSource() == jbu5) {
			// 下重置
			jt3.setText("");
			jt4.setText("");
			jt5.setText("");
			jt6.setText("");
			jt7.setText("");
			jt8.setText("");
			jte.setText("");
			toggleBtn.setText("禁用");
			toggleBtn.setSelected(false);
			conn.reset(gr);
			RefreshTable();
		} else if (e.getSource() == jbu4) {
			// 删除
			if (id.equals("")) {
				JOptionPane.showMessageDialog(null, "未选择删除的用户！", "消息", JOptionPane.WARNING_MESSAGE);
			} else {
				String sql = "delete from tb_reader where readerid='" + id + "'";
				int i = JOptionPane.showConfirmDialog(null, "确认删除嘛？", "消息", JOptionPane.YES_OPTION);
				if (i == 0) {
					int g = readerdao.dataChange(sql);
					if (g > 0) {
						JOptionPane.showMessageDialog(null, "删除成功！", "消息", JOptionPane.INFORMATION_MESSAGE);
						RefreshTable();
						jt3.setText("");
						jt4.setText("");
						jt5.setText("");
						jt6.setText("");
						jt7.setText("");
						jt8.setText("");
						jte.setText("");
						toggleBtn.setText("禁用");
						toggleBtn.setSelected(false);
						conn.reset(gr);
					} else {
						JOptionPane.showMessageDialog(null, "删除失败！", "消息", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		} else if (e.getSource() == jbu3) {
			// 修改
			if (id.equals("")) {
				JOptionPane.showMessageDialog(null, "未选择要修改的用户！", "消息", JOptionPane.WARNING_MESSAGE);
			} else {
				String sql = "update tb_reader set readername='" + name + "',readerpass='" + pwd + "',readersex='" + n
						+ "',readerdate='" + tiem + "',readertel='" + phone + "',readeraddress='" + address
						+ "',readerstatus='" + t + "',readerremark='" + bz + "' where readerid='" + id + "' ";
				int i = JOptionPane.showConfirmDialog(null, "确认修改嘛？", "消息", JOptionPane.YES_OPTION);
				if (i == 0) {
					int h = readerdao.dataChange(sql);
					if (h > 0) {
						JOptionPane.showMessageDialog(null, "修改成功！", "消息", JOptionPane.INFORMATION_MESSAGE);
						RefreshTable();
						if (t.equals("1")) {
							HttpClientUtil client = HttpClientUtil.getInstance();
							String Key = "你自己的key";
							String Uid = "自己的用户ID";
							String smsMob = phone;
							String smsText = "尊敬的用户，您申请的图书管理系统已通过审核，您的登入账户是：" + id + " 密码是：" + pwd + "";
							int res = client.sendMsgGbk(Uid, Key, smsText, smsMob);
							if (res > 0) {
								JOptionPane.showMessageDialog(null, "短信发送成功！", "消息", JOptionPane.INFORMATION_MESSAGE);
							} else {
								JOptionPane.showMessageDialog(null, "短信发送失败！", "消息", JOptionPane.ERROR_MESSAGE);
							}
						}
					} else {
						JOptionPane.showMessageDialog(null, "修改失败！", "消息", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		} else if (e.getSource() == jbu1) {
			// 查询
			String rename = jt1.getText();
			String rephone = jt2.getText();
			if (rename.equals("") && rephone.equals("")) {
				RefreshTable();
			} else if (rephone.equals("")) {
				String sql = "select * from tb_reader where readername like'%" + rename + "%'";
				String[] colnames = { "编号", "读者姓名", "密码", "性别", "注册时间", "联系电话", "联系地址", "会员状态", "备注信息" };
				ArrayList plist = readerdao.getRecordsDataBySql(sql);
				String[][] rowdatas = readerdao.ListToArray(plist);
				tableModel.setDataVector(rowdatas, colnames);
			} else if (rename.equals("")) {
				String sql = "select * from tb_reader where readertel like'%" + rephone + "%'";
				String[] colnames = { "编号", "读者姓名", "密码", "性别", "注册时间", "联系电话", "联系地址", "会员状态", "备注信息" };
				ArrayList plist = readerdao.getRecordsDataBySql(sql);
				String[][] rowdatas = readerdao.ListToArray(plist);
				tableModel.setDataVector(rowdatas, colnames);
			} else {
				String sql = "select * from tb_reader  where readertel like'%" + rephone + "%' or readername like'%"
						+ rename + "%' ";
				String[] colnames = { "编号", "读者姓名", "密码", "性别", "注册时间", "联系电话", "联系地址", "会员状态", "备注信息" };
				ArrayList plist = readerdao.getRecordsDataBySql(sql);
				String[][] rowdatas = readerdao.ListToArray(plist);
				tableModel.setDataVector(rowdatas, colnames);
			}

		}
	}

	private void RefreshTable() {// 刷新
		// TODO Auto-generated method stub
		String[] colnames = { "编号", "读者姓名", "密码", "性别", "注册时间", "联系电话", "联系地址", "会员状态", "备注信息" };
		String sql = "select * from tb_reader";
		ArrayList plist = readerdao.getRecordsDataBySql(sql);
		String[][] rowdatas = readerdao.ListToArray(plist);
		tableModel.setDataVector(rowdatas, colnames);

	}

}
